<template>
  <div>
    <el-table ref="tableRef" border size="mini" fit :data="tableData">
      <!-- 序号 -->
      <el-table-column v-if="isIndex" type="index" label="序号" align="center" width="60">
        <template slot-scope="scope"><span>{{ scope.row.typeIndex }}</span></template>
      </el-table-column>
      <!-- 表格数据 -->
      <template v-for="(colItem, colIndex) in cols">
        <el-table-column
          :key="colIndex"
          :align="colItem.align ? colItem.align :'center'"
          :prop="colItem.propName"
          :label="colItem.label"
          :width="colItem.width"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <img v-if="colItem.propName == 'imageUrl'" width="36px" height="36px" :src="scope.row.imageUrl">
            <template v-else-if="colItem.propName == 'lession_type'">
              <el-tag v-if="scope.row.lession_type === 1" type="primary" effect="dark">线上</el-tag>
              <el-tag v-if="scope.row.lession_type === 2" type="info" effect="dark">线下</el-tag>
            </template>
            <template v-else-if="colItem.propName == 'is_free'">
              <el-tag v-if="scope.row.is_free === 2" type="primary">收费</el-tag>
              <el-tag v-else type="info">免费</el-tag>
            </template>
            <template v-else-if="colItem.propName == 'is_custom'">
              <span v-if="scope.row.is_custom == 2">不可以</span>
              <span v-else-if="scope.row.is_custom == 1">可以</span>
            </template>
            <template v-else>
              {{ scope.row[colItem.propName] }}
            </template>
          </template>
        </el-table-column>
      </template>
      <!-- 操作栏 -->
      <el-table-column label="操作" align="center" fixed="right" width="220" :resizable="false">
        <template slot-scope="scope">
          <div class="comtable-operator-content">
            <template v-for="val in handleData">
              <el-button v-if="val == 'check'" :key="val" size="small" type="primary" icon="el-icon-search" @click="handleFunc(scope, val)">查看</el-button>
              <el-button v-if="val == 'recover'" :key="val" size="small" type="success" icon="el-icon-delete" @click="handleFunc(scope, val)">恢复</el-button>
              <el-button v-if="val == 'edit'" :key="val" size="small" type="primary" icon="el-icon-edit" @click="handleFunc(scope, val)">编辑</el-button>
              <el-button v-if="val == 'delete'" :key="val" size="small" icon="el-icon-delete" type="danger" @click="handleFunc(scope, val)">删除</el-button>
            </template>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="bottomPaginationa"
      :total="total"
      :page-size="pageSize"
      :current-page="page"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
    props: {
        // 是否存在序号列
        isIndex: {
            type: Boolean,
            default: true
        },
        // 列表数据
        tableData: {
            type: Array,
            default() {
                return []
            }
        },
        // 列数据
        cols: {
            type: Array,
            default() {
                return []
            }
        },
        // 操作栏数据
        handleData: {
            type: Array,
            default() {
                return ['edit', 'delete', 'check', 'recover']
            }
        },
        // 表格数据总条数
        total: {
            type: [Number, String],
            default: 0
        },
        // 表格当前页数
        pageNumber: {
            type: [Number, String],
            default: 1
        }
    },
    data() {
        return {
            page: 1,
            pageSize: 10
        }
    },
    watch: {
        tableData(newVal) {
            this.tableData = newVal
            if (this.tableData.length > 0) {
                this.tableData.forEach((item, index) => {
                    item['typeIndex'] = Number(index + (this.page - 1) * this.pageSize + 1)
                })
            }
            this.$forceUpdate()
        },
        total(newVal) {
            this.total = Number(newVal)
        },
        pageNumber(newVal) {
            this.page = Number(newVal)
        }
    },
    methods: {
        // 表格操作事件
        handleFunc(event, buttonType) {
            event.buttonType = buttonType
            this.$emit('handleFunc', event)
        },
        // 当前页显示条目个数改变事件
        handleSizeChange(val) {
            this.page = 1
            this.pageSize = val
            this.$emit('getList', {
                limit: val,
                page: 1
            })
        },
        // 当前页数改变事件
        handleCurrentChange(val) {
            this.page = val
            this.$emit('getList', {
                page: val
            })
        }
    }
}
</script>
<style scoped lang="scss">
  .bottomPaginationa {
    display: flex;
    margin-top: 15px;
    justify-content: flex-end;
  }
</style>
